<?php
// 应用公共文件
/**
 * header log
 *  more like   hlog($var,$var2,$var3...);
 * @param ...$argv
 * @return void
 */
function hlog(...$argv)
{

    // handle 1 $label
    $label = 'unnamed';
    // hlog('var_name',$var), hlog('var_name',$var,$var2,$var3...)
    if (count($argv) > 1 && is_string($argv[0]) && strlen($argv[0])) {
        $label = $argv[0];
        array_shift($argv);
    }
    // handle 2 $vars
    foreach ($argv as $item) {
        \app\common\HeaderLog::log($label, $item,1);
    }
}

//
function generateCreateTableSql($tableName, $array)
{
    if (isset($array[0]) && is_array($array[0])) {
        $array = $array[0];
    }
    $sql = "CREATE TABLE `$tableName` (\n";
    $sql .= "  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,\n";

    foreach ($array as $key => $value) {
        if ('id' == $key) {
            continue;
        }
        $type = gettype($value);
        $columnType = '';

        switch ($type) {
            case 'integer':
                $columnType = 'INT UNSIGNED';
                break;
            case 'array':
                $columnType = 'JSON';
                break;
            default:
                $decimals = strlen(substr(strrchr((string)$value, '.'), 1));
                if (is_numeric($value) && 2 <= $decimals) {
                    $columnType = "DECIMAL(32, 18) ";
                } else {
                    $columnType = 'VARCHAR(255)';
                }
                $rawValue = var_export($value, true);
                if (strlen($rawValue) > 200) {
                    $columnType = 'longtext ';
                }

                break;
        }

        $sql .= "  `$key` $columnType,\n";
    }

    $sql .= "  PRIMARY KEY (`id`)\n";
    $sql .= ");";

    return $sql;
}

function thow_if($condition, $message)
{
    if ($condition) {
        throw new \Exception($message);
    }
}


function crc64Table()
{
    $crc64tab = [];

    // ECMA polynomial
    $poly64rev = (0xC96C5795 << 32) | 0xD7870F42;

    // ISO polynomial
    // $poly64rev = (0xD8 << 56);

    for ($i = 0; $i < 256; $i++)
    {
        for ($part = $i, $bit = 0; $bit < 8; $bit++) {
            if ($part & 1) {
                $part = (($part >> 1) & ~(0x8 << 60)) ^ $poly64rev;
            } else {
                $part = ($part >> 1) & ~(0x8 << 60);
            }
        }

        $crc64tab[$i] = $part;
    }

    return $crc64tab;
}
/**
 * @param string $string
 * @param string $format
 * @return mixed
 *
 * Formats:
 *  crc64('php'); // afe4e823e7cef190
 *  crc64('php', '0x%x'); // 0xafe4e823e7cef190
 *  crc64('php', '0x%X'); // 0xAFE4E823E7CEF190
 *  crc64('php', '%d'); // -5772233581471534704 signed int
 *  crc64('php', '%u'); // 12674510492238016912 unsigned int
 */
function crc64($string, $format = '%u')
{
    static $crc64tab;

    if ($crc64tab === null) {
        $crc64tab = _crc64_table();
    }

    $crc = 0;

    for ($i = 0; $i < strlen($string); $i++) {
        $crc = $crc64tab[($crc ^ ord($string[$i])) & 0xff] ^ (($crc >> 8) & ~(0xff << 56));
    }

    return sprintf($format, $crc);
}
function _crc64_table(){
    return[
        0 => 0,
        1 => -5535402513002963089,
        2 => -827476681416466341,
        3 => 5164075066763771700,
        4 => 8921845837811637811,
        5 => -3963573138538102436,
        6 => -8118593940182008216,
        7 => 4357999468653093127,
        8 => -603052398086275994,
        9 => 4940391307328217865,
        10 => 226782375002905661,
        11 => -5761232158350294190,
        12 => -8326798863640698283,
        13 => 4566377562367245626,
        14 => 8715998937306186254,
        15 => -3757340862016250527,
        16 => 9051005139383707209,
        17 => -3551671569944921818,
        18 => -8565961459053115886,
        19 => 4193374422961527165,
        20 => 453564750005811322,
        21 => -5375839991167752427,
        22 => -950447627940620255,
        23 => 4747102235666401102,
        24 => -8486428553008784849,
        25 => 4113029525020509504,
        26 => 9132755124734491252,
        27 => -3634302816408165093,
        28 => -1014746199097179108,
        29 => 4811156168024382323,
        30 => 391483189436228679,
        31 => -5314072338612520152,
        32 => -344733794942137198,
        33 => 5195199925788447741,
        34 => 1131375642422963401,
        35 => -4855662593294912602,
        36 => -9158208430687022431,
        37 => 3731739485546663374,
        38 => 8386748845923054330,
        39 => -4085333180854407787,
        40 => 907129500011622644,
        41 => -4631800727367372901,
        42 => -571126819714445137,
        43 => 5421418680781082560,
        44 => 8594564625313771207,
        45 => -4294100590368099928,
        46 => -8952539602376749412,
        47 => 3525329033817543155,
        48 => -8742362874173347109,
        49 => 3855837706121835956,
        50 => 8226059050041019008,
        51 => -4537770656272329233,
        52 => -181233824240569112,
        53 => 5643692520190618503,
        54 => 718348998302913715,
        55 => -4983696819872789540,
        56 => 8146277531524994749,
        57 => -4457674130217743918,
        58 => -8824431737660786970,
        59 => 3938150108875254153,
        60 => 782966378872457358,
        61 => -5047431839805663263,
        62 => -118903857361917739,
        63 => 5582173445676054458,
        64 => 7257036000092981153,
        65 => -2911463407282235186,
        66 => -8056344222132656134,
        67 => 2529986302517213333,
        68 => 2262751284845926802,
        69 => -6032390349762361603,
        70 => -1449351927949395511,
        71 => 6398650419759490726,
        72 => -7847613871801156665,
        73 => 2322133910755632296,
        74 => 7463478971093326748,
        75 => -3117099887985244941,
        76 => -1673246381863442956,
        77 => 6622864283287239323,
        78 => 2036569382881248687,
        79 => -5805960506456565056,
        80 => 1814259000023245288,
        81 => -6195890629502321017,
        82 => -1321317598487363149,
        83 => 6811676960462675676,
        84 => 7132938157145702363,
        85 => -3327309341956448076,
        86 => -7603906712147386496,
        87 => 2690676064372932847,
        88 => -1257614823082009202,
        89 => 6747026957542163169,
        90 => 1875814858707893717,
        91 => -6258183708997999942,
        92 => -7684039816217820227,
        93 => 2770420489343360210,
        94 => 7050658067635086310,
        95 => -3245207924841710455,
        96 => -6953160100862932173,
        97 => 3219832958944941148,
        98 => 7711675412243671912,
        99 => -2870179086519323641,
        100 => -1994625973627513600,
        101 => 6305011443818121839,
        102 => 1213047649942025563,
        103 => -6630476404036343244,
        104 => 7503259434831574869,
        105 => -2662012149972555718,
        106 => -7159359033328314610,
        107 => 3425713581329221729,
        108 => 1436697996605827430,
        109 => -6854934340521691639,
        110 => -1768758650736473795,
        111 => 6078267261889762898,
        112 => -2154189010659562118,
        113 => 5851447209550246421,
        114 => 1630020308903038241,
        115 => -6507505285908540850,
        116 => -7365062116336110775,
        117 => 3090674103720225830,
        118 => 7876300217750508306,
        119 => -2422811326922453891,
        120 => 1565932757744914716,
        121 => -6443240161887138189,
        122 => -2215918504504708793,
        123 => 5913566482019610152,
        124 => 7956607163135676207,
        125 => -2502382151029190592,
        126 => -7282397182357442700,
        127 => 3008957496780927003,
        128 => -3932672073523589310,
        129 => 8809633696146542637,
        130 => 4460922918905818905,
        131 => -8158783662249152394,
        132 => -5567412237929787023,
        133 => 113391187501452830,
        134 => 5059972605034426666,
        135 => -786178333796749755,
        136 => 4525502569691853604,
        137 => -8222556824080028597,
        138 => -3870308643033771137,
        139 => 8748148222884465680,
        140 => 4980157760350383383,
        141 => -706115546429410696,
        142 => -5649443234190570164,
        143 => 195741594718114339,
        144 => -5406581602485245685,
        145 => 565687821211481700,
        146 => 4644267821511264592,
        147 => -910417325212854721,
        148 => -3519786131522898120,
        149 => 8937808626997553239,
        150 => 4297282312656885603,
        151 => -8607135623245150196,
        152 => 4852190599768102253,
        153 => -1119077323475416574,
        154 => -5201015507135072970,
        155 => 359174499151456857,
        156 => 4073138765762497374,
        157 => -8383170749551946703,
        158 => -3746286292604474619,
        159 => 9163920108173816938,
        160 => 3628518000046490576,
        161 => -9118283621180465985,
        162 => -4116532283263851637,
        163 => 8498696072880078052,
        164 => 5299565100954197475,
        165 => -385731908190223732,
        166 => -4823390152784200264,
        167 => 1018284691440624343,
        168 => -4180867759418146890,
        169 => 8562713237611094233,
        170 => 3566469078572851181,
        171 => -9056483741914333054,
        172 => -4743889748392664699,
        173 => 937907429353946858,
        174 => 5381352128745865694,
        175 => -468326524461261135,
        176 => 5746791986423309721,
        177 => -220966226947081482,
        178 => -4952690158625225278,
        179 => 606523824971012781,
        180 => 3751629717415787434,
        181 => -8701451563069430587,
        182 => -4569956191557559311,
        183 => 8338992711486538910,
        184 => -5160786708676208129,
        185 => 815010154451519120,
        186 => 5540840978686720420,
        187 => -14837645541906741,
        188 => -4345427938439378996,
        189 => 8115412784602421411,
        190 => 3978303581567838103,
        191 => -8927389306748356360,
        192 => -5919282011750233885,
        193 => 2230461459452909452,
        194 => 6439665917889882296,
        195 => -1553734490144933929,
        196 => -3023393249222207792,
        197 => 7288217715337890239,
        198 => 2490078880175191691,
        199 => -7953140121649534492,
        200 => 6520081235612152965,
        201 => -1633197079553807382,
        202 => -5836721186073307938,
        203 => 2148641156328442801,
        204 => 2426095299884051126,
        205 => -7888771163999816231,
        206 => -3085231252839216403,
        207 => 7350228890552538498,
        208 => -3440225204046401878,
        209 => 7165105895222849989,
        210 => 2649782550477098737,
        211 => -7499716522796904034,
        212 => -6084047658828648295,
        213 => 1783234539286425590,
        214 => 6851427162658443458,
        215 => -1424434862061826131,
        216 => 2873395993211654860,
        217 => -7724211225838613085,
        218 => -3214325240990928233,
        219 => 6938393941075996152,
        220 => 6642978682516671743,
        221 => -1216300290739711088,
        222 => -6290209549930025820,
        223 => 1989151790783919051,
        224 => 6263731030979658865,
        225 => -1890541448826905826,
        226 => -6743849654609058774,
        227 => 1245039440087595845,
        228 => 3260040617806076482,
        229 => -7056101485762165459,
        230 => -2757949010207720935,
        231 => 7680756410435167606,
        232 => -6823875760881862633,
        233 => 1324891275238549368,
        234 => 6181348207440451660,
        235 => -1808542903113677021,
        236 => -2694143638208535004,
        237 => 7616209416359311691,
        238 => 3321489341258335871,
        239 => -7118501837995222768,
        240 => 3131865515489829432,
        241 => -7468987255756522153,
        242 => -2309597564811247005,
        243 => 7844397531750915340,
        244 => 5811434156413844491,
        245 => -2051371843948305564,
        246 => -6619611109670331312,
        247 => 1660744670629167935,
        248 => -2533529747438199202,
        249 => 8068573254449152305,
        250 => 2905717078206922245,
        251 => -7242523810129747606,
        252 => -6410914086585843603,
        253 => 1452858539103461122,
        254 => 6017914993561854006,
        255 => -2256970321264951463,
    ];
}
